package com.dst.smart.handler.disposer;

import android.content.Context;
import android.util.Log;
import com.dst.smart.app.MainActivity;
import com.dst.smart.handler.entity.SemanticResult;
import com.dst.smart.handler.entity.ServiceType;
import com.dst.smart.player.entity.SongPlayInfo;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * 谜语。
 *
 * 	猜谜语
 	来一个字谜
 * 
 * author <a href="http://www.xfyun.cn">讯飞开放平台</a>
 * date 2016年9月5日 下午4:58:10
 *
 */
public class RiddleDisposer extends Disposer {
	private static final String KEY_URL = "url";

	public RiddleDisposer(Context context) {
		super(context);
	}
	
	@Override
	public void disposeResult(final SemanticResult semanticResult) {
		try {
			if (semanticResult != null) {
				JSONArray result = semanticResult.getResult();
				if (result != null && result.length() != 0) {
					//String resultTxt=semanticResult.getAnswerText();
					//MainActivity.playSound(5, semanticResult.getAnswerText());
					//先播放標題
					List<SongPlayInfo> playList = new ArrayList<SongPlayInfo>();
					SongPlayInfo songPlayInfo;
					if (semanticResult.getAnswerText().length() > 0) {
						songPlayInfo = new SongPlayInfo();
						songPlayInfo.setPlayUrl(semanticResult.getAnswerText());
						playList.add(songPlayInfo);
					}
					for (int i = 0; i < result.length(); i++) {
						JSONObject jsonObject = result.getJSONObject(i);
						String title = "第" + (i + 1) + "題." + jsonObject.getString("title");
						String answer = "答案:" + jsonObject.getString("answer");
						//resultTxt+=title+answer;
						songPlayInfo = new SongPlayInfo();
						songPlayInfo.setPlayUrl(title + answer);
						playList.add(songPlayInfo);
					}
					getPlayController().playURLList(semanticResult.getUUID(), playList);
					getPlayController().playText(semanticResult.getUUID(), playList.get(0).getPlayUrl(), true, "", null);
					sendAiuiData("谜语", "riddle", result.toString());
				} else {
					getPlayController().playURLList(semanticResult.getUUID(), null);
					getPlayController().playText(semanticResult.getUUID(), semanticResult.getAnswerText(), true, "", null);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean canHandle(ServiceType type){
		return (type == ServiceType.RIDDLE);
	}
}
